package org.shopping.service.commodity.impl;

import org.shopping.dao.commodity.CommodityDao;
import org.shopping.dao.commodity.impl.CommodityDaoImpl;
import org.shopping.domain.Commodity;
import org.shopping.domain.PageBean;
import org.shopping.service.commodity.CommodityService;

import java.util.List;

public class CommodityServiceImpl implements CommodityService {

    // 创建dao对象
    private CommodityDao dao = new CommodityDaoImpl();

    @Override
    public List<Commodity> selectAll() {
        return dao.selectAll();
    }

    @Override
    public PageBean<Commodity> findPageByCurrentPage(Integer currentPage, Integer pageSize) {
        //1.创建PageBean对象,封装分页数据
        PageBean<Commodity> pageBean = new PageBean<Commodity>();
        //2.封装当前页码数字
        pageBean.setCurrentPage(currentPage);
        //3.封装每页显示的具体条数
        pageBean.setPageSize(pageSize);
        //4.封装每页显示的具体数据
        List<Commodity> list = dao.findByPage(currentPage, pageSize);
        pageBean.setList(list);
        //5.封装总的条数
        Number number = dao.findTotalCount();
        int totalCount =number.intValue();
        pageBean.setTotalCount(totalCount);
        //6.封装总的页码数: 总的条数101  每页显示10条
        Integer totalPage  = (totalCount%pageSize==0)?
                totalCount/pageSize : ((totalCount/pageSize)+1);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public boolean deleteCommodity(String cidForm) {
        // 调用dao层删除方法
        int i = dao.deleteCommodity(cidForm);
        if (i==1) {
            // 影响了一行，删除成功
            return true;
        }
        return false;
    }

    @Override
    public Commodity findOneCommodity(String cidForm) {
        // 调用dao层查找方法
        Commodity commodity = dao.findCommodity(cidForm);
        if (commodity != null) {
            // 找到了
            return commodity;
        }
        // 没找到
        return null;
    }

    @Override
    public boolean updateCommodity(Commodity commodity, String updateCid) {
        // 调用dao层方法
        int row = dao.updateCommodity(commodity, updateCid);
        if (row > 0) {
            // 修改成功
            return true;
        }
        return false;
    }

    @Override
    public boolean addCommodity(Commodity commodity) {
        // 执行dao层方法
        int i = dao.addCommodity(commodity);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
